<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Translated Triangle</title>
</head>

<body onload="main()">
    <canvas id="webGl" width="400" height="400">
        Please use a browser that supports 'canvas'
    </canvas>
    <script src="../lib/webgl-utils.js"></script>
    <script src="../lib/webgl-debug.js"></script>
    <script src="../lib/cuon-utils.js"></script>
    <script>
        // 顶点着色器程序
        var VSHADER_SOURCE =
            'attribute vec4 a_Position;\n' +
            'uniform float u_CosB;\n' +
            'uniform float u_SinB;\n' +
            'void main() {\n' +
            ' gl_Position.x = a_Position.x * u_CosB - a_Position.y * u_SinB;\n' + // 设置x坐标
            ' gl_Position.y = a_Position.x * u_SinB + a_Position.y * u_CosB;\n' + // 设置y坐标
            ' gl_Position.z = a_Position.z;\n' + // 设置z坐标
            ' gl_Position.w = a_Position.w;\n' + // 设置w坐标
            '}\n';
        // 片元着色器程序
        var FSHADER_SOURCE =
            'precision mediump float;\n' +  // 精度声明
            'uniform vec4 u_FragColor;\n' +
            'void main() {\n' +
            ' gl_FragColor = u_FragColor;\n' + // 设置颜色
            '}\n';

        var ANGLE = 135;

        function main() {
            var canvas = document.getElementById('webGl');

            var gl = getWebGLContext(canvas);

            if (!gl) {
                console.log('Failed to get the rending context for WebGL!');
                return;
            }

            // 初始化着色器
            if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
                console.log('Failed to initialize shaders.');
                return;
            }

            // 获取attribute变量的存储位置
            var u_FragColor = gl.getUniformLocation(gl.program, 'u_FragColor'),
                u_CosB = gl.getUniformLocation(gl.program, 'u_CosB'),
                u_SinB = gl.getUniformLocation(gl.program, 'u_SinB');

            if (!u_FragColor || !u_CosB || !u_SinB) {
                console.log('Failed to get u_FragColor/u_CosB/u_SinB variable');
                return;
            }

            // 设置顶点位置
            var n = initVertexBuffers(gl);

            if (n < 0) {
                console.log('Failed to set the positions of the vertices');
                return;
            }

            // 将旋转图形所需的数据传输给顶点着色器
            var radian = Math.PI * ANGLE / 180.0; // 转为弧度
            var cosB = Math.cos(radian);
            var sinB = Math.sin(radian);

            gl.uniform1f(u_CosB, cosB);
            gl.uniform1f(u_SinB, sinB);
            gl.uniform4f(u_FragColor, 1.0, 0.0, 1.0, 1.0);

            // 设置<canvas>的背景色
            gl.clearColor(0.0, 0.0, 0.0, 1.0);

            // 清空canvas
            gl.clear(gl.COLOR_BUFFER_BIT);

            // 绘制n个点
            gl.drawArrays(gl.TRIANGLE_STRIP, 0, n);
        }

        function initVertexBuffers(gl) {

            var vertices = new Float32Array([
                -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, -0.5
            ]);
            var n = 4;//点的个数

            // 创建缓冲区对象
            var vertexBuffer = gl.createBuffer();
            if (!vertexBuffer) {
                console.log('Failed to create the buffer object');
                return -1;
            }

            // 将缓冲区对象绑定到目标
            gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);

            // 向缓冲区对象写入数据
            gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);

            var a_Position = gl.getAttribLocation(gl.program, 'a_Position');

            if (a_Position < 0) {
                console.log('Failed to get the storage location of a_Position');
                return -1;
            }

            // 将缓冲区对象分配给a_Position变量
            gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);

            // 连接a_Position变量与分配给它的缓冲区对象
            gl.enableVertexAttribArray(a_Position);

            return n;
        }
    </script>
</body>

</html>